#include <Wire.h>
#include <SPI.h>

#include <Usb.h>
#include <AndroidAccessory.h>

#define LED 13

#define ZCEN1 49
#define CS1   48
#define MOSI  51
#define SC    52
#define MUTE1 47
#define ZCEN2 46
#define CS2   45
#define MUTE2 44

AndroidAccessory acc("HIDAKA Takahiro",
                     "SceneMix",
                     "SceneMix Arduino Board",
                     "0.1",
                     "http://www.airdroid.org",
                     "0000000000000001");

int count = 0;
int vf = 0;
int w = 0;

void initSPI()
{
    pinMode(ZCEN1, OUTPUT);
    digitalWrite(ZCEN1, HIGH);
    pinMode(CS1, OUTPUT);
    digitalWrite(CS1, HIGH);
    pinMode(MUTE1, OUTPUT);
    digitalWrite(MUTE1, HIGH);
  
    pinMode(ZCEN2, OUTPUT);
    digitalWrite(ZCEN2, HIGH);
    pinMode(CS2, OUTPUT);
    digitalWrite(CS2, HIGH);
    pinMode(MUTE2, OUTPUT);
    digitalWrite(MUTE2, HIGH);

    SPI.begin();    
    SPI.setDataMode(SPI_MODE0);
    SPI.setBitOrder(MSBFIRST);
    SPI.setClockDivider(SPI_CLOCK_DIV32);
 
    delay(10);  

    digitalWrite(CS1, LOW);

    delay(10);  

    SPI.transfer(140);
    SPI.transfer(140);

    delay(10);  
    
    digitalWrite(CS1, HIGH);
}

void setup()
{
    digitalWrite(LED, HIGH);
    pinMode(LED, OUTPUT);
    
    initSPI();
    
    acc.powerOn();
}

void loop()
{
    count++;
    if (acc.isConnected()) {
        if (count / 10 % 2 == 0) {
            digitalWrite(LED, HIGH);
        } else {
            digitalWrite(LED, LOW);
        }
    } else {
        if (count / 200 % 2 == 0) {
            digitalWrite(CS1, LOW);
            delay(10);
            SPI.transfer(140);
            SPI.transfer(0);
            delay(10);
            digitalWrite(CS1, HIGH);
        } else {
            digitalWrite(CS1, LOW);
            delay(10);
            SPI.transfer(0);
            SPI.transfer(140);
            delay(10);
            digitalWrite(CS1, HIGH);
        }
    }
      
    delay(10);  
}

